package dyy.zc.sportruler.ui.activity;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;

import java.util.ArrayList;
import java.util.List;

import dyy.zc.sportruler.R;
import dyy.zc.sportruler.base.BaseActivity;
import dyy.zc.sportruler.bean.SendPlan;
import dyy.zc.sportruler.listener.OnItemClickListener;
import dyy.zc.sportruler.ui.adapter.SendAdapter;
import dyy.zc.sportruler.utils.BmobUtils;
import dyy.zc.sportruler.utils.DividerItemDecoration;
import dyy.zc.sportruler.views.EmptyView;

public class SendActivity extends BaseActivity {
    private TextView title;
    private RecyclerView recyclerView;
    private SendAdapter adapter;
    private List<SendPlan> list = new ArrayList<>();
    private BmobUtils bmobUtils;
    private EmptyView emptyView;
    private boolean isRequestData = true;
    private MaterialRefreshLayout materialRefreshLayout;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    dialog.dismiss();
                    Log.e("list",list.size()+"");
                    if (list.size()==0){
                        isRequestData = false;
                        emptyView.setNotify("暂无数据");
                    }else {
                        isRequestData = true;
                        emptyView.setEmptyViewGone();
                        adapter.setList(list);
                        adapter.notifyDataSetChanged();
                    }
                    materialRefreshLayout.finishRefreshLoadMore();
                    materialRefreshLayout.finishRefresh();
                    break;
            }
        }
    };
    @Override
    protected void findViewByIDS() {
        emptyView = new EmptyView(this);
        title = findViewsById(R.id.top_tv_title);
        recyclerView = findViewsById(R.id.recyclerView);
        materialRefreshLayout = findViewsById(R.id.materialRefreshLayout);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        initData();
        bind();
    }

    private void bind() {
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

            }
        });

        materialRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                bmobUtils.getSendPlanInfo(list,loginuser.getId(),1,handler);
            }

            @Override
            public void onRefreshLoadMore(final MaterialRefreshLayout materialRefreshLayout) {
                if (isRequestData) {
                    bmobUtils.getSendPlanInfo(list,loginuser.getId(),1,handler);
                    return;
                }
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        materialRefreshLayout.finishRefreshLoadMore();
                    }
                }, 1000);
            }
        });
    }

    private void initData() {
        title.setText("发送过的计划");
        bmobUtils = new BmobUtils(this);
        adapter = new SendAdapter(this,list);
        DividerItemDecoration decoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST);
        decoration.setDivider(R.drawable.divider_shape);
        recyclerView.addItemDecoration(decoration);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        dialog = ProgressDialog.show(this,null,"正在获取数据.....");
        bmobUtils.getSendPlanInfo(list,loginuser.getId(),1,handler);
    }
}
